/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function handleTree(data: any, id: string, parentId: string, children: string) {
    let config = {
      id: id || 'id',
      parentId: parentId || 'parentId',
      childrenList: children || 'children'
    }

    var childrenListMap: any = {}
    var tree = []
    for (let d of data) {
      let id = d[config.id]
      childrenListMap[id] = d
      if (!d[config.childrenList as unknown as string]) {
        d[config.childrenList as unknown as string] = []
      }
    }

    for (let d of data) {
      let parentId = d[config.parentId]
      let parentObj = childrenListMap[parentId]
      if (!parentObj) {
        tree.push(d)
      } else {
        parentObj[config.childrenList as unknown as string].push(d)
      }
    }
    return tree
  }